package com.panopset.refactorsf;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.panopset.UpdateListener;

/**
 *
 * Validating date field, format <b>yyyy/MM/dd</b>.
 * @author Karl Dinwiddie
 *
 */
public class ValidatingDateField extends ValidatingField<Date>{

    /**
     *
     * @param name
     *            Name of field, used in alerts when not valid.
     */
    public ValidatingDateField(final String name) {
        super(name);
        setDate(new Date());
    }

    @Override
    public Date getValue() throws Exception {
        return FORMAT.parse(getTf().getText());
    }

    @Override
    public boolean validate(String s) {
        try {
            getValue();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void save() {
        // TODO Auto-generated method stub
        
    }

    public void setDate(Date date) {
        setValue(FORMAT.format(date));
    }
    
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy/MM/dd");

}
